// toast
function Toast(title, status) {
	let icon = status ? 'success' : 'none'
	uni.showToast({
		title: title,
		mask: !0,
		icon: icon,
		duration: 1500
	})
}
// 删除数组指定字符串
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};
// 富文本转纯文本

function convertHtmlToText(inputText) {
	var returnText = "" + inputText;
	returnText = returnText.replace(/<\/div>/ig, '\r\n');
	returnText = returnText.replace(/<\/li>/ig, '\r\n');
	returnText = returnText.replace(/<li>/ig, '  *  ');
	returnText = returnText.replace(/<\/ul>/ig, '\r\n');
	//-- remove BR tags and replace them with line break
	returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

	//-- remove P and A tags but preserve what's inside of them
	returnText = returnText.replace(/<p.*?>/gi, "\r\n");
	returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

	//-- remove all inside SCRIPT and STYLE tags
	returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
	returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
	//-- remove all else
	returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

	//-- get rid of more than 2 multiple line breaks:
	returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

	//-- get rid of more than 2 spaces:
	returnText = returnText.replace(/ +(?= )/g, '');

	//-- get rid of html-encoded characters:
	returnText = returnText.replace(/&nbsp;/gi, " ");
	returnText = returnText.replace(/&amp;/gi, "&");
	returnText = returnText.replace(/&quot;/gi, '"');
	returnText = returnText.replace(/&lt;/gi, '<');
	returnText = returnText.replace(/&gt;/gi, '>');

	return returnText;
}
// 验证手机号码
function re_mobile(mobile) {
	return /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1}))+\d{8})$/.test(
		mobile)
}
// 获取时间
function getT(t) {
	let myDate = new Date(t);
	let year = myDate.getFullYear();
	let month = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
	let day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
	let hour = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
	let minute = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
	let second = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
	let this_time = year + '-' + month + '-' + day
	return this_time
}
// 获取时间
function getTime(t) {
	let myDate = t ? new Date(t) : new Date();
	let year = myDate.getFullYear();
	let month = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
	let day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
	let hour = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
	let minute = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
	let second = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
	let this_time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
	return this_time
}
// 获取时间
function getTimes(t) {
	let myDate = new Date(t);
	let year = myDate.getFullYear();
	let month = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
	let day = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
	let hour = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
	let minute = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
	let second = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
	let this_time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
	return this_time
}
// 获取时间
function getSeckillTime(t) {
	let second = t % 60 < 10 ? '0' + t % 60 : t % 60
	let minute = parseInt(t / 60) % 60 < 10 ? '0' + parseInt(t / 60) % 60 : parseInt(t / 60) % 60
	let hour = parseInt(t / 3600) % 24 < 10 ? '0' + parseInt(t / 3600) % 24 : parseInt(t / 3600) % 24
	let day = parseInt(t / 86400) < 10 ? '0' + parseInt(t / 86400) : parseInt(t / 86400)
	return day + '天' + hour + '时' + minute + '分' + second + '秒'
}
// base64加密
function Base64() {

	// private property  
	let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

	// public method for encoding  
	this.encode = function(input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
		input = _utf8_encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output +
				_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
				_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
		}
		return output;
	}

	// public method for decoding  
	this.decode = function(input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = _keyStr.indexOf(input.charAt(i++));
			enc2 = _keyStr.indexOf(input.charAt(i++));
			enc3 = _keyStr.indexOf(input.charAt(i++));
			enc4 = _keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = _utf8_decode(output);
		return output;
	}

	// private method for UTF-8 encoding  
	let _utf8_encode = function(string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}
		return utftext;
	}

	// private method for UTF-8 decoding  
	let _utf8_decode = function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while (i < utftext.length) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

/**
 * 乘法
 * @param arg1
 * @param arg2
 * @returns {Number}
 */
function accMul(arg1, arg2) {
	var m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {}
	try {
		m += s2.split(".")[1].length
	} catch (e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}
/**
 * 除法
 * @param arg1
 * @param arg2
 * @returns {Number}
 */
function accDiv(arg1, arg2) {
	var t1 = 0,
		t2 = 0,
		r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {}
	r1 = Number(arg1.toString().replace(".", ""))
	r2 = Number(arg2.toString().replace(".", ""))
	return (r1 / r2) * pow(10, t2 - t1);
}
/**
 * 加法
 * @param arg1
 * @param arg2
 * @returns {Number}
 */
function accAdd(arg1, arg2) {
	var r1, r2, m, c;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	c = Math.abs(r1 - r2);
	m = Math.pow(10, Math.max(r1, r2))
	if (c > 0) {
		var cm = Math.pow(10, c);
		if (r1 > r2) {
			arg1 = Number(arg1.toString().replace(".", ""));
			arg2 = Number(arg2.toString().replace(".", "")) * cm;
		} else {
			arg1 = Number(arg1.toString().replace(".", "")) * cm;
			arg2 = Number(arg2.toString().replace(".", ""));
		}
	} else {
		arg1 = Number(arg1.toString().replace(".", ""));
		arg2 = Number(arg2.toString().replace(".", ""));
	}
	return (arg1 + arg2) / m
}
/**
 * 减法
 * @param arg1
 * @param arg2
 * @returns
 */
function accSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	//last modify by deeka
	//动态控制精度长度
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
export {
	getT,
	getTime,
	getTimes,
	Base64,
	Toast,
	getSeckillTime,
	accMul,
	accDiv,
	accAdd,
	accSub,
	convertHtmlToText
}
